<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    style="margin-top: -15px"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      pagination
      background
      border
      height="740"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <operate :id="id" :visible.sync="show" @close="close"></operate>
  </lb-layout>
</template>

<script>
import { thresholdManageList, thresholdManageDel } from "@/api/sub/portrayal";
import CascaderSelect from "@/components/Select/CascaderSelect";
import RemoteSelect from "@/components/Select/RemoteSelect";
import operate from "@/views/configuration/arithmetic/portrayal/components/thresholdOperate";
export default {
  name: "threshold",
  components: {
    CascaderSelect,
    RemoteSelect,
    operate,
  },
  data() {
    return {
      sysDeptIdChose: null,
      sysDeptId: [],
      netwoekChose: null,
      id: "",
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "100px",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            // date: {
            //   typeName: "el-date-picker",
            //   type: "daterange",
            //   size: "small",
            //   "value-format": "yyyy-MM-dd",
            //   "start-placeholder": "创建开始日期",
            //   "end-placeholder": "创建结束日期",
            // },
            sysDeptId: {
              label: "用能单位:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                return (
                  <div>
                    <CascaderSelect
                      value={that.sysDeptId}
                      values={that.sysDeptId}
                      varient={"energy"}
                      size={"small"}
                      placeholder={"请选择"}
                      onInput={that.handlerLiandong}
                    ></CascaderSelect>
                  </div>
                );
              },
            },
            deviceMonitoringNetworksId: {
              label: "能耗终端:",
              typeName: "el-select",
              render: () => {
                let that = this;
                return (
                  <div>
                    <RemoteSelect
                      v-model={that.netwoekChose}
                      varient="networks"
                      extraParams={that.extraParams}
                      placeholder={"请选择"}
                      size={"small"}
                      onInput={that.choseNetwork}
                    ></RemoteSelect>
                  </div>
                );
              },
            },
            // meterDeviceCode: {
            //   typeName: "el-input",
            //   placeholder: "阈值代码",
            // },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "新增",
              type: "primary",
              typeName: "el-button",
              plain: true,
              size: "small",
              icon: "el-icon-plus",
              click: () => this.operate(""),
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "sysDeptName",
            label: "用能单位",
            showOverflowTooltip: true,
            minWidth: 100,
          },
          {
            prop: "deviceMonitoringNetworksName",
            label: "能耗终端",
            minWidth: 100,
            showOverflowTooltip: true,
          },
          {
            prop: "meterDeviceCode",
            label: "计量设备",
            minWidth: 100,
            showOverflowTooltip: true,
          },
          {
            prop: "energyThreshold",
            label: "能耗阈值",
            minWidth: 100,
            showOverflowTooltip: true,
          },
          {
            label: "操作",
            width: "150",
            fixed: "right",
            render: (h, scope) => {
              return (
                <div>
                  {
                    <el-button
                      v-hasPermi={["device:networks:edit"]}
                      type="text"
                      size="small"
                      icon="el-icon-edit"
                      onClick={() => {
                        this.operate(scope.row.id);
                      }}
                    >
                      修改
                    </el-button>
                  }
                  {
                    <el-button
                      v-hasPermi={["device:networks:remove"]}
                      type="text"
                      size="small"
                      icon="el-icon-delete"
                      onClick={() => {
                        this.handleOperDelete(scope.row);
                      }}
                      style="color:red"
                    >
                      删除
                    </el-button>
                  }
                </div>
              );
            },
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    operate(id) {
      this.id = id;
      this.show = true;
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {
        ...data,
      };
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      this.$store.commit("app/updateApiCount", "add");
      await thresholdManageList(query)
        .then((res) => {
          this.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          this.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!301003");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      // const { date, ...other } = this.$refs?.form?.getFieldsValue();
      // const [startTime, endTime] =
      //   this.$refs?.form?.getFieldsValue().date || [];
      const { ...other } = this.$refs?.form?.getFieldsValue();
      const data = {
        ...other,
        sysDeptId: this.sysDeptIdChose,
        deviceMonitoringNetworksId: this.netwoekChose,
        // endTime,
        // startTime,
      };
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val, flag) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val, flag) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    //重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    // 联动
    handlerLiandong(value) {
      this.netwoekChose = null;
      if (value && value.length > 0) {
        this.sysDeptIdChose = value[value.length - 1];
      } else {
        this.sysDeptIdChose = null;
      }
      // 请求能耗终端
      this.extraParams = {
        sysDeptId: this.sysDeptIdChose,
      };
    },
    /* 选择能耗终端 */
    choseNetwork(value) {
      this.netwoekChose = value;
    },
    // 导出
    handleDerive() {},
    //删除
    handleOperDelete(row) {
      let that = this;
      const text = "是否确认删除" + (row?.name ?? "该条数据") + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.$store.commit("app/updateApiCount", "add");
          thresholdManageDel({ id: row.id })
            .then((res) => {
              that.$store.commit("app/updateApiCount");
              if (res.code === 700) {
                that.$message.warning(res.msg);
                return false;
              }

              that.$message.success("已删除!");
              that.initData();
            })
            .catch((err) => {
              that.$store.commit("app/updateApiCount");
              // this.$message.error("系统错误,请联系管理员!301017");
            });
        })
        .catch(() => {});
    },
  },
};
</script>
